/**
 * 
 */
package org.arpit.masteringextjs.rs;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.arpit.masteringextjs.dao.impl.FilmDAOImpl;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author arpit
 * 
 */
@Path("/json/film")
public class FimResource {

	@GET
	@Path("/findAll")
	@Produces(MediaType.APPLICATION_JSON)
	public String findAll() throws JsonGenerationException,
			JsonMappingException, IOException {
		System.out.println("Inside FimResource findAll!!");

		ObjectMapper mapper = new ObjectMapper();
		String json = "";

		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("data", FilmDAOImpl.getInstance().findAll());

		// convert map to JSON string
		json = mapper.defaultPrettyPrintingWriter().writeValueAsString(map);
		// System.out.println(json);
		return json;
	}

	@POST
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public void create(@FormParam("film_id") String id,
			@FormParam("title") String title,
			@FormParam("release_year") int releaseDate,
			@FormParam("language_id") String languageId,
			@FormParam("original_language_id") String orignalLanguageId,
			@FormParam("rental_duration") String rentalDuration,
			@FormParam("rental_rate") String rentalRate,
			@FormParam("replacement_cost") String replacementCost,
			@FormParam("rating") String rating,
			@FormParam("trailers") String trailers,
			@FormParam("commentaries") String commentaries,
			@FormParam("behind") String behind,
			@FormParam("description") String description) {
		
		System.out.println(">>>>>" + id);
		System.out.println(">>>>>" + title);
		System.out.println(">>>>>" + releaseDate);
		System.out.println(">>>>>" + languageId);
		System.out.println(">>>>>" + orignalLanguageId);
		System.out.println(">>>>>" + rentalDuration);
		System.out.println(">>>>>" + rentalRate);
		System.out.println(">>>>>" + replacementCost);
		System.out.println(">>>>>" + rating);
		System.out.println(">>>>>" + trailers);
		System.out.println(">>>>>" + commentaries);
		System.out.println(">>>>>" + behind);
		System.out.println(">>>>>" + description);
	}

}
